<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

require_once(dirname(__FILE__) . "/../common/Logger.php");
require_once(dirname(__FILE__) . "/../common/Common.php");
require_once(dirname(__FILE__) . "/../dbcom/OrderCom.php");
require_once(dirname(__FILE__) . "/../dbcom/ServiceinfoCom.php");
require_once(dirname(__FILE__) . "/../dbcom/ServiceCom.php");

//$log = new Logger("./../service.log");
$common = new Common();
$ordercom = new OrderCom();
$serviceinfocom = new ServiceinfoCom();
$servicecom = new ServiceCom();

if (!isset($_REQUEST['userid']) || empty($_REQUEST['userid'])) {
    echo json_encode(array("code" => 100, "msg" => "cannot find userid"));
    exit();
}

$userid = $_REQUEST['userid'];

$olist = $ordercom->getAllOrdermByCid($userid);
if(!$olist){
    echo json_encode(array("code" => 400, "msg" => "find order fail"));
    exit();
}

$idstr = "";
if(!empty($olist)){
    $a = array();
    foreach($olist as $k=>$v){
        $a[$k] = $v['id'];
    }
    $idstr = implode(",", $a);
}

$services = $serviceinfocom->getServiceinfoByOids($idstr);
//var_dump($services);
$array = array("code"=>0,"data"=>array());
if(!empty($services)){
    $kk = 0;
    foreach($services as $vv){
        if(($vv['total'] - $vv['used_num'])>0){
            $val = $vv['total'] - $vv['used_num'];
            $service = $servicecom->getServiceById($vv['service_id']);
            $array['data'][$kk]['num'] = $val;
            $array['data'][$kk]['service'] = $service['name'];
            $myorder = $ordercom->getOrderById($vv['order_id']);
            $array['data'][$kk]['createtime'] = date("Y-m-d",$myorder['create_time']);
            $array['data'][$kk]['tradeno'] = $myorder['trade_no'];
            $kk++;
        }
    }
}

echo json_encode($array);
exit();
